package cn.kgc.mybatis.web.servlet;

import cn.kgc.mybatis.web.dto.EmpPageDTO;
import cn.kgc.mybatis.web.entity.Emp;
import cn.kgc.mybatis.web.service.EmpService;
import cn.kgc.mybatis.web.service.impl.EmpServiceImpl;
import cn.kgc.mybatis.web.utils.JSONUtil;
import cn.kgc.mybatis.web.utils.ResponseResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author 课工场
 * @date 2024/1/29
 * @description emp请求的处理
 * <p>
 * 1.分页查询  select   *  from  emp
 * 2.搜索     ename  age    select   *  from  emp  where  ...
 * 3.添加员工
 * 4.删除员工
 * 5.修改员工
 * 6.查询员工
 * <p>
 * get  post
 * <p>
 * /emp?method=add
 * /emp?method=delete
 * /emp?method=update
 * /emp?method=select
 */
@WebServlet("/emp")
public class EmpServlet extends BaseServlet {

    private EmpService empService = new EmpServiceImpl();

    //  员工信息添加
    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO  调用业务层  实现数据的添加   ResponseResult
        //  响应结果给前端页面   /list  ->转发 重定向的  实现页面跳转    json 轻量级的数据传输格式  {username:'tom',age:20}
        //  {code:200,message:'提示信息',data:{}}

        // 获取参数
        String ename = req.getParameter("ename");
        String age = req.getParameter("age");
        String deptno = req.getParameter("deptno");

        Emp emp = new Emp();
        emp.setEname(ename);

        if (age!=null){
            emp.setAge(Integer.valueOf(age));
        }

        if (deptno!=null){
            emp.setDeptno(Integer.valueOf(deptno));
        }

        ResponseResult responseResult =  empService.addEmp(emp);
        //  调用工具类 将响应数据写出
        JSONUtil.writeJson(responseResult,resp);
    }

    //  员工信息批量删除
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String[] ids = req.getParameterValues("id");
        List<String> list = Arrays.asList(ids);

        ResponseResult responseResult =   empService.batchDelete(list);
        JSONUtil.writeJson(responseResult,resp);

    }

    //  员工信息修改
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取参数
        String ename = req.getParameter("ename");
        String age = req.getParameter("age");
        String deptno = req.getParameter("deptno");
        String id = req.getParameter("id");

        Emp emp = new Emp();
        emp.setEname(ename);

        if (age!=null){
            emp.setAge(Integer.valueOf(age));
        }

        if (deptno!=null){
            emp.setDeptno(Integer.valueOf(deptno));
        }

        if (id!=null){
            emp.setId(Integer.valueOf(id));
        }

        ResponseResult responseResult = empService.editEmp(emp);

        JSONUtil.writeJson(responseResult,resp);

    }

    //  员工信息查询  分页
    protected void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        // 1.获取参数
        String currentPage = req.getParameter("currentPage");
        String size = req.getParameter("size");
        String ename = req.getParameter("ename");
        String age = req.getParameter("age");

        // 2.数据封装
        EmpPageDTO empPageDTO = new EmpPageDTO();
        if (currentPage!=null){
            empPageDTO.setCurrentPage(Integer.valueOf(currentPage));
        }

        if (size!=null){
            empPageDTO.setSize(Integer.valueOf(size));
        }

        empPageDTO.setEname(ename);

        if (age!=null){
            empPageDTO.setAge(Integer.valueOf(age));
        }

        ResponseResult   responseResult = empService.searchEmp(empPageDTO);
        JSONUtil.writeJson(responseResult,resp);
    }

}
